package pannelli.sub;

import java.awt.Dimension;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

import oggetti.Cliente;
import pannelli.VisualizzaCliente;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import database.DB;
import java.awt.event.*;
import oggetti.NonEditableDefaultTableModel;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class SubPannelloVisualizzaClienti extends javax.swing.JPanel {

    private JScrollPane jScrollPane1;
    private DefaultTableModel model;
    private boolean flag;
    private JTable tabellaClienti;
    private int row;
    private static SubPannelloVisualizzaClienti instance;
    private VisualizzaCliente v;

    private SubPannelloVisualizzaClienti() {
        super();
        v = null;
        initGUI();
        flag = false;
    }

    public static SubPannelloVisualizzaClienti getInstance() {
        if (instance == null) {
            instance = new SubPannelloVisualizzaClienti();
        }
        return instance;
    }

    public int getCurrentRow() {
        return row;
    }

    public int getMaxRow() {
        return tabellaClienti.getRowCount();
    }

    public Cliente getClienteFromRow(int row) {
        String id = (String) tabellaClienti.getValueAt(row, 0);
        String nome = (String) tabellaClienti.getValueAt(row, 1);
        String cognome = (String) tabellaClienti.getValueAt(row, 2);
        String residenza = (String) tabellaClienti.getValueAt(row, 3);
        String telefono = (String) tabellaClienti.getValueAt(row, 4);
        String iva = (String) tabellaClienti.getValueAt(row, 5);
        String fiscale = (String) tabellaClienti.getValueAt(row, 6);
        String mail = (String) tabellaClienti.getValueAt(row, 7);

        Cliente c = new Cliente(nome, cognome, iva, fiscale, residenza, telefono, mail, Integer.parseInt(id), "", "");
        return c;
    }

    public void svuotaTabella() {
        for (int i = 0; i < model.getRowCount(); i++) {
            model.removeRow(i);
        }
    }

    public void initTable(ArrayList<Cliente> c) {
        int a = model.getRowCount();
        for (int i = 0; i < a; i++) {
            model.removeRow(0);
        }

        for (Cliente tmp : c) {
            model.addRow(tmp.toArray());
        }
    }

    public void initTable() {
        int a = model.getRowCount();
        for (int i = 0; i < a; i++) {
            model.removeRow(0);
        }

        ArrayList<Cliente> c = new DB().getClienti();
        if (c != null) {
            for (Cliente tmp : c) {
                model.addRow(tmp.toArray());
            }
        }

    }

    private void initGUI() {
        try {
            AnchorLayout thisLayout = new AnchorLayout();
            this.setLayout(thisLayout);
            this.setPreferredSize(new java.awt.Dimension(835, 300));
        } catch (Exception e) {
            e.printStackTrace();
        }
        model = new NonEditableDefaultTableModel();
        tabellaClienti = new JTable(model);
        tabellaClienti.setPreferredSize(new java.awt.Dimension(822, 298));
        jScrollPane1 = new JScrollPane();
        this.add(jScrollPane1, new AnchorConstraint(1, 1000, 1001, 0, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(799, 264));
        jScrollPane1.setViewportView(tabellaClienti);
        String[] columnNames = {"ID",
            "Nome", "Cognome", "Residenza", "Telefono", "IVA", "Codice Fiscale", "E-Mail"};
        for (int i = 0; i < columnNames.length; i++) {
            model.addColumn(columnNames[i]);
        }

        tabellaClienti.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent arg0) {
                if (arg0.getClickCount() >= 2) {
                    if(v!=null){
                       v.setVisible(false);
                       v = null;
                       flag = false;
                    }
                    if (!flag) {
                        flag = true;
                        row = tabellaClienti.getSelectedRow();
                        if (row >= 0) {
                            v = new VisualizzaCliente(getClienteFromRow(row), row, getMaxRow());
                            v.addWindowListener(new WindowAdapter() {

                                public void windowClosing(WindowEvent arg0) {
                                    flag = false;
                                }
                            });
                        }
                    }
                }
            }
        });

        tabellaClienti.getColumnModel().getColumn(0).setPreferredWidth(30);
        tabellaClienti.setPreferredSize(new java.awt.Dimension(831, 295));

    }
}
